package com.ihd.ihardwaretools.view.model;

import android.app.Application;
import android.os.Looper;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.ihd.ihardwaretools.api.HttpClient;
import com.ihd.ihardwaretools.common.AppConstans;
import com.ihd.ihardwaretools.pojo.EmptyRes;
import com.ihd.ihardwaretools.pojo.ListRes;
import com.ihd.ihardwaretools.pojo.LoginRes;

import org.reactivestreams.Publisher;

import cn.wowjoy.commonlibrary.app.Constans;
import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MRepository extends BaseRepository {

    public void login(String username, String password) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("name", username);
        mParam.addProperty("passwords", password);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().login(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<LoginRes>(Constans.LOGIN) {
                }));
    }

    public void sendVerifySms(String phone, String type) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("mobile", phone);
        mParam.addProperty("type", type);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().sendVerifySms(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(Constans.CODES) {
                }));
    }

    public void resetPwd(String phone, String password, String smsCode) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("mobile", phone);
        mParam.addProperty("password", password);
        mParam.addProperty("smsCode", smsCode);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().resetPwd(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<LoginRes>(Constans.RESET) {
                }));
    }

    public void bind(Application mApplication, String originalData, String customerId, String productionLine) {
        String token = SPUtils.getString(Constans.GATEWAY_TOKEN, "");
        JsonObject jo = new JsonParser().parse(token).getAsJsonObject();

        JsonObject mParam = new JsonObject();
        mParam.addProperty("adminId", jo.get("userId").getAsString());
        mParam.addProperty("originalData", originalData);
        mParam.addProperty("customerId", customerId);
        mParam.addProperty("productionLine", productionLine);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().bind(rb)
                .flatMap(new Function<EmptyRes, Publisher<ListRes>>() {
                    @Override
                    public Publisher<ListRes> apply(EmptyRes emptyRes) throws Exception {
                        if (emptyRes.getCode() == 0)
                            return HttpClient.getService().queryList(jo.get("userId").getAsString(), 1, 5, "", "");
                        else {
                            Looper.prepare();
                            ToastUtils.showShort(mApplication.getApplicationContext(), emptyRes.getMessage());
                            Looper.loop();
                            return Flowable.empty();
                        }
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<ListRes>(AppConstans.LIST) {
                }));
    }

    public void list(int pageNum, int pageSize, String beginModifyTime, String endModifyTime) {
        String token = SPUtils.getString(Constans.GATEWAY_TOKEN, "");
        JsonObject jo = new JsonParser().parse(token).getAsJsonObject();
        addDisposable(HttpClient.getService().queryList(jo.get("userId").getAsString(), pageNum, pageSize, beginModifyTime, endModifyTime)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<ListRes>(AppConstans.LIST) {
                }));
    }
}
